import type { Router} from 'vue-router';
import { useTitle } from '@vueuse/core';
import nprogress from 'nprogress';
import { useRouteStore } from '@/store';
import { routerGuard } from './utils';
/**
 * createRouterGuard 全局路由守卫
 */
export async function createRouterGuard(router: Router){
  const route = useRouteStore();
  router.beforeEach(async (to, form, next) => {
    nprogress.start();
    // if(to.name === 'not-found-page'){
    //   next({ name: 'abnormal_404' });
    // }
    const permission = await routerGuard(to, next);
    if(!permission) return;
    next();
  });

  router.afterEach((to) => {
    const title = useTitle();
    title.value = to?.meta?.title as string;
    nprogress.done();
  });
}
